class Solution {
public:

    void dfs(vector<int>& candidates, int target, int start, vector<vector<int>>& ans, vector<int>& temp)
    {
        if (target == 0)
        {
            ans.push_back(temp);
            return;
        }
        if (target < 0)
            return;
        for (int i = start; i < candidates.size(); ++i)
        {
            if (i > start && candidates[i] == candidates[i - 1])
                continue;
            if (target - candidates[i] >= 0)
            {
                temp.push_back(candidates[i]);
                dfs(candidates, target - candidates[i], i + 1, ans, temp);
                temp.pop_back();
            }
        }
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<vector<int>>ans;
        vector<int>temp;

        dfs(candidates, target, 0, ans, temp);
        return ans;
    }
};